package com.wk.controller.student;

import com.wk.pojo.Stu_map;
import com.wk.pojo.Tea_map;
import com.wk.pojo.User;
import com.wk.service.Mapservice;
import com.wk.utils.responseresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 学生的地图类
 * @author: 王侃
 * @create: 2020-03-06 09:56
 **/
@Controller
@RequestMapping("/stu")
public class Stumapcontroller {

    @Autowired
    Mapservice mapservice;

    //跳转到签到列表界面
    @RequestMapping("/map/show")
    public String map_show(HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Tea_map> maps=mapservice.selectbyclass_id_map(user.getClass_id());
        model.addAttribute("maps",maps);
        return "student/map/map_show";
    }

    //跳转到签到界面
    @RequestMapping("/map/confirm/{id}")
    public String map_submit(@PathVariable("id")Integer id,HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        Integer flag=mapservice.selectbydeadline_id(id);
        if (flag<0)
        {

            List<Tea_map> maps=mapservice.selectbyclass_id_map(user.getClass_id());
            model.addAttribute("maps",maps);
            model.addAttribute("map_id",id);
            model.addAttribute("msg","已经超过签到截至时间了哦");
            return "student/map/map_show";
        }
        else {
            Stu_map stu_map=mapservice.selectbyuser_id_tm_id(user.getId(),id);
            if (stu_map!=null)
            {
                List<Tea_map> maps=mapservice.selectbyclass_id_map(user.getClass_id());
                model.addAttribute("maps",maps);
                model.addAttribute("map_id",id);
                model.addAttribute("msg","同学你已经签过到了哦");
                return "student/map/map_show";
            }
        }
        Tea_map tea_map=mapservice.selectbyidtea_map(id);
        model.addAttribute("tea_map",tea_map);
        return "student/map/map_confirm";
    }

    //学生签到确认界面
    @RequestMapping("/map/confirmsubmit")
    public String map_submit(String location, Stu_map map, HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        String location1=location.substring(0,location.indexOf(","));
        String location2=location.substring(location.lastIndexOf(",")+1);

        map.setStu_id(user.getId());
        map.setStu_name(user.getUsername());
        map.setClass_id(user.getClass_id());
        //通过主键id获得信息
        Tea_map tea_map=mapservice.selectbyidtea_map(map.getTea_map_id());

        //传入成功定位的经纬度
        model.addAttribute("mylocation_a",location1);
        model.addAttribute("mylocation_b",location2);

        model.addAttribute("required_a",tea_map.getLocation_1());
        model.addAttribute("required_b",tea_map.getLocation_2());
        model.addAttribute("distance",tea_map.getDistance());
        model.addAttribute("tea_map_id",tea_map.getId());
        model.addAttribute("map",map);
        return "student/map/map_submit";
    }

    //学生签到信息添加到数据库
    @RequestMapping("/map/confirm/addsubmit")
    @ResponseBody
    public responseresult confirm_addsubmit(Integer tea_id,String tea_name,Integer status,Integer class_id,Integer tea_map_id,HttpSession session)
    {
        User user=(User)session.getAttribute("loginuser");

        //防止学生在页面停留过久，这里再做一次验证，看是否超时，如果超时则签到失败
        Date time=new Timestamp(new Date().getTime());
        Integer flag=mapservice.checknow_time(time,tea_map_id);
        if (flag<0)
            status=0;

        Stu_map map=new Stu_map();
        map.setClass_id(class_id);
        map.setStatus(status);
        map.setTea_id(tea_id);
        map.setTea_name(tea_name);
        map.setTea_map_id(tea_map_id);
        map.setStu_id(user.getId());
        map.setStu_name(user.getUsername());

        mapservice.insertintoStu_map(map);
        return responseresult.success();
    }
}
